<template>
  <div>
    <editForm
      :formConfig="basicConfig"
      ref="basicForm"
      :formData="formData"
    ></editForm>
  </div>
</template>

<script>
export default {
  props: {
    allInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    unEdit: {
      type: Boolean,
      default: false,
    },
    pageType: {
      type: String,
    },
  },
  data() {
    return {
      show: false,
      disabled: false,
      basicConfig: {
        labelWidth: "140px",
        itemArr: [
          {
            label: "巡视类别",
            prop: "patrolCategory",
            type: "select",
            required: true,
            disabled: false,
            span: 8,
            selectOption: this.$store.getters.dirtData("patrol_way"),
          },
          {
            label: "所属单位",
            prop: "planDeptName",
            propArr: "planDeptArr",
            idsStr: "planDeptId",
            type: "dept-select",
            option: [],
            required: true,
            disabled: false,
            span: 8,
          },
          {
            label: "巡视单位",
            prop: "patrolDeptName",
            propArr: "patrolDeptArr",
            idsStr: "patrolDeptId",
            type: "dept-select",
            option: [],
            required: true,
            disabled: false,
            span: 8,
          },
          {
            label: "人员安排方式",
            prop: "staffingWay",
            type: "select",
            required: true,
            isHide: false,
            span: 8,
            selectOption: this.$store.getters.dirtData("staffing_way"),
            methods: (val) => {
              if (val.value == "appoint") {
                this.basicConfig.itemArr[4].isHide = true;
                this.basicConfig.itemArr[5].isHide = false;
                this.$refs.basicForm.setFormItem("locationName", null);
                this.$refs.basicForm.setFormItem("locationId", null);
              } else {
                this.basicConfig.itemArr[4].isHide = false;
                this.basicConfig.itemArr[5].isHide = true;
                this.$refs.basicForm.setFormItem("workerName", null);
                this.$refs.basicForm.setFormItem("workerId", null);
                this.$refs.basicForm.setFormItem("workers", []);
              }
            },
          },
          {
            label: "值班地点",
            prop: "locationName",
            propArr: "locationArr",
            idsStr: "locationId",
            type: "location-select",
            option: [],
            multiple: false,
            isHide: false,
            required: true,
            disabled: false,
            span: 8,
          },
          {
            label: "巡视人员",
            prop: "workerName",
            propArr: "workerArr",
            idsStr: "workerId",
            type: "userOnly-select",
            option: [],
            multiple: true,
            required: true,
            disabled: false,
            isHide: false,
            span: 8,
            methods: (data) => {
              let workers = data.map((item) => {
                const obj = {
                  workerId: item.id,
                  workerName: item.name,
                };
                return obj;
              });
              this.$refs.basicForm.setFormItem("workers", workers);
            },
          },
          {
            label: "签到方式",
            prop: "arriveWay",
            type: "select",
            required: true,
            disabled: false,
            span: 8,
            selectOption: [],
          },
          {
            label: "完成时必须签名",
            prop: "mustSign",
            type: "select",
            required: true,
            span: 8,
            selectOption: [
              { label: "是", value: 1 },
              { label: "否", value: 0 },
            ],
          },
          {
            label: "准备工作",
            prop: "readyWork",
            type: "textarea",
            rows: 4,
            required: false,
            span: 24,
          },
          {
            label: "定时下达",
            prop: "isTimer",
            type: "select",
            required: true,
            span: 8,
            selectOption: [
              { label: "是", value: 1 },
              { label: "否", value: 0 },
            ],
            methods: (val) => {
              let value = val.value == 0 ? true : false;
              this.basicConfig.itemArr[10].isHide = value;
              this.$emit("unTimeRelease", value);
            },
          },
          {
            label: "提前下达小时",
            prop: "advanceHours",
            type: "el-number",
            min: 0,
            required: true,
            span: 8,
          },
          {
            label: "巡视时长(小时)",
            prop: "patrolDuration",
            type: "el-number",
            min: 0,
            required: true,
            span: 8,
          },
          {
            label: "备注",
            prop: "remark",
            type: "text",
            required: false,
            span: 8,
          },
        ],
      },
      formData: {
        mustSign: 0,
      },
      filesList: [],
    };
  },
  created() {
    let option = this.$store.getters.dirtData("arrive_way");
    option.pop();
    this.$set(this.basicConfig.itemArr[6], "selectOption", option);
    if (this.unEdit) {
      this.basicConfig.itemArr.map((i) => (i.disabled = true));
    } else {
      this.basicConfig.itemArr.map((i) => (i.disabled = false));
    }
  },
  mounted() {
    if (this.pageType == "add") {
      this.formData = {
        planDeptName:this.$store.getters.userInfo.orgName,
        planDeptId:this.$store.getters.userInfo.orgId,
        patrolDeptName:this.$store.getters.userInfo.orgName,
        patrolDeptId:this.$store.getters.userInfo.orgId
      }
      let deptOption = [
        {
          label: this.$store.getters.userInfo.orgName,
          value: this.$store.getters.userInfo.orgId,
          id: this.$store.getters.userInfo.orgId,
        },
      ];
      setTimeout(() => {
        this.$refs.basicForm.setItemOption("planDeptName", deptOption);
        this.$refs.basicForm.setItemOption("patrolDeptName", deptOption);
        this.$refs.basicForm.setFormItem(
          "planDeptArr",
          this.$store.getters.userInfo.orgId
        );
        this.$refs.basicForm.setFormItem(
          "patrolDeptArr",
          this.$store.getters.userInfo.orgId
        );
      }, 100);
    }
  },
  watch: {
    allInfo: {
      handler(val) {
        this.init(val);
      },
    },
    unEdit: {
      handler(val) {
        if (val) {
          this.basicConfig.itemArr.map((i) => (i.disabled = true));
        } else {
          this.basicConfig.itemArr.map((i) => (i.disabled = false));
        }
      },
    },
  },
  methods: {
    //初始化函数
    init(val) {
      let data = JSON.parse(JSON.stringify(val));
      let workerName = data.workers.map((item) => item.workerName).join(",");
      let workerId = data.workers.map((item) => item.workerId).join(",");
      let workerArr = data.workers.map((item) => item.workerId);
      data.workerName = workerName;
      data.workerId = workerId;
      this.formData = data;
      let planDeptOption = [
        {
          label: this.formData.planDeptName,
          value: this.formData.planDeptId,
          id: this.formData.planDeptId,
        },
      ];
      let patrolDeptOption = [
        {
          label: this.formData.patrolDeptName,
          value: this.formData.patrolDeptId,
          id: this.formData.patrolDeptId,
        },
      ];
      setTimeout(() => {
        this.$refs.basicForm.setItemOption("planDeptName", planDeptOption);
        this.$refs.basicForm.setItemOption("patrolDeptName", patrolDeptOption);
        this.$refs.basicForm.setFormItem(
          "planDeptArr",
          this.formData.planDeptId
        );
        this.$refs.basicForm.setFormItem(
          "patrolDeptArr",
          this.formData.patrolDeptId
        );
      }, 100);
      if (val.staffingWay == "appoint") {
        this.basicConfig.itemArr[4].isHide = true;
        this.basicConfig.itemArr[5].isHide = false;
        let workerOption = data.workers.map((item) => {
          let obj = {
            label: item.workerName,
            value: item.workerId,
            id: item.workerId,
            account:item.workerId,
            name: item.workerName
          };
          return obj;
        });
        setTimeout(() => {
          this.$refs.basicForm.setItemOption("workerName", workerOption);
          this.$refs.basicForm.setFormItem("workerArr", workerArr);
        }, 100);
      } else {
        this.basicConfig.itemArr[4].isHide = false;
        this.basicConfig.itemArr[5].isHide = true;
        let locationOption = [
          {
            label: this.formData.locationName,
            value: this.formData.locationId,
            id: this.formData.locationId,
          },
        ];
        setTimeout(() => {
          this.$refs.basicForm.setItemOption("locationName", locationOption);
        }, 0);
        setTimeout(() => {
          this.$refs.basicForm.setFormItem(
            "locationArr",
            this.formData.locationId
          );
        }, 100);
      }
      this.basicConfig.itemArr[10].isHide = val.isTimer == 1 ? false : true;
    },
    async getBasicInfo() {
      try {
        let formData = await this.$refs.basicForm.getFormData();
        return formData;
      } catch (error) {}
    },
  },
};
</script>

<style lang="less" scoped>
.upload-file {
  margin-left: 70px;
  text-align: left;
  display: flex;
  span {
    margin-right: 10px;
  }
}
</style>